package com.globaldom.web.sales;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;

import javax.jdo.PersistenceManager;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.globaldom.model.CivResource;
import com.globaldom.model.MilResource;
import com.globaldom.model.Sales;
import com.globaldom.utils.Constants;
import com.globaldom.utils.MemCacheManager;
import com.globaldom.utils.PMF;
import com.globaldom.utils.SessionManager;

@Controller
@RequestMapping(value = "/addsales")
public class SalesAddController {
	public SalesAddController() {
	}
	private static final Logger log = Logger.getLogger(SalesAddController.class.getName());

	/*
	 */
	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public @ResponseBody
	Map<String, String> addSales(@RequestParam int saleType, @RequestParam long saleNum, @RequestParam int salePrice,
			HttpSession session, HttpServletRequest request) {
		Map<String, String> map = new HashMap<String, String>();
		String personID = SessionManager.getID(request, session);
		if(null==personID) 
			{
			map = new HashMap<String, String>();
			map.put(Constants.STATUS, "-ER");
			map.put(Constants.CODE, "401");
			return map;
			}
		
		Sales sale = new Sales();
	try {

			sale.setSaleAmount(saleNum);
			sale.setSaleType(saleType);
			sale.setSalePrice(salePrice);
			sale.setCreateDate(new Date());
			sale.setPerson(personID);
			PersistenceManager pmf = PMF.get().getPersistenceManager();
			pmf.makePersistent(sale);
			pmf.close();

			if (saleType < 6) {
				// set military resource here
				PersistenceManager pm = PMF.get().getPersistenceManager();
				 MilResource mil = pm.getObjectById(MilResource.class, personID +"/" + saleType);
				long avail = mil.getMilAvailable() - saleNum;
				if (avail < 0)
					throw new RuntimeException("Military Number out of range!");
				mil.setMilAvailable(avail);
				pm.close();
				MemCacheManager.removeFreeResources(personID);
			} else if (saleType < 12) {
				CivResource cv = null;
				PersistenceManager pm = PMF.get().getPersistenceManager();
				cv = pm.getObjectById(CivResource.class, personID +"/" + (saleType-6));
				
				long avail = cv.getResAvailable() - saleNum;
				if (avail < 0)
					throw new RuntimeException("Civilian number out of range!");
				cv.setResAvailable(avail);
				pm.close();
				MemCacheManager.addCivic(personID, saleType - 6, avail);
			}

			map.put(Constants.STATUS, "+OK");
			map.put(Constants.CODE, "" + sale.getSalePrice());
		} catch (Exception ex) {
			log.severe("Error during transaction" + ex);
			map.put(Constants.STATUS, "+-ER");
			map.put(Constants.CODE, ex.getLocalizedMessage());
		} finally {
			
		}
		// MemCacheManager.addSale(sale);
		return map;
	}

}
